package org.example.sword2offer.primary;

/**
 * @author: lynn
 * @Descript: 不使用 replace 实现,StringBuilder.疑难杂症就在字符串扩容
 * Question 2
 * @date: 2021/3/7 19:02
 * @version: 1.0
 */
public class Q2_ReplaceSpace {
    public static void main(String[] args) {
        String  targetStr="I am a java coder.";
        System.out.println(replaceSpace(targetStr));
    }
    public static String replaceSpace(String target){
        char[] targetChar=target.toCharArray();
        StringBuilder sb=new StringBuilder();
        for (int i=0;i<targetChar.length;i++){
            if (targetChar[i]==' '){
                //扩容
                sb.append('%');
                sb.append('2');
                sb.append('0');
            }else {
                sb.append(targetChar[i]);
            }
        }
        return sb.toString();
    }
}
